package com.ct.controller.login;

import com.ct.application.sysuser.contants.SysUserContants;
import com.ct.application.sysuser.model.vo.SysUserVO;
import com.ct.application.sysuser.request.SysUserRequest;
import com.ct.application.sysuser.service.ISystemUserService;
import com.ct.commons.BaseController;
import com.ct.commons.annotation.ApiLog;
import com.ct.redis.util.RedisUtil;
import com.ct.utils.jwt.JwtUtil;
import com.ct.utils.threadlocal.UserThreadLocal;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 后台登录
 */
@RestController
public class AdminLoginController extends BaseController {


    @Resource
    private ISystemUserService systemUserService;
    @Resource
    private RedisUtil<String> redisUtil;

    /**
     * 登录
     * @param sysUserRequest
     * @return
     */
    @ApiLog("登录")
    @PostMapping("/login/toLogin")
    public SysUserVO adminLogin(@Validated @RequestBody SysUserRequest sysUserRequest){
        return systemUserService.adminLogin(sysUserRequest);
    }

    /**
     * 退出
     * @return
     */
    @PostMapping("/login/logout")
    @ApiLog("退出")
    public Boolean logout(){
        // 删掉原来登录的token
        redisUtil.del(SysUserContants.LOGIN_REDIS_TOKEN_KEY+ UserThreadLocal.get().getId());
        // 把删掉的token移植到小黑屋
        redisUtil.set(SysUserContants.LOGIN_REDIS_BLACKLIST_TOKEN_KEY+UserThreadLocal.get().getId(),UserThreadLocal.get().getToken(), JwtUtil.EXPIRE_TIME, TimeUnit.MILLISECONDS);
        return true;
    }
}
